package edu.tomee.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil {

	public static Session getMailSession(){
		Session session = null;
		Properties properties = System.getProperties();//???wht proprties is it getting frm system
		// Setup mail server
		properties.setProperty("mail.smtp.host", "smtp.kbase.in");
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", "587");

		session = Session.getDefaultInstance(properties,new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("admin@kbase.in","openhouse123$");
			}
		});
		return session;
	}
	public static void sendMessage( String recipientEmail, String msg, String subject ){
		Session session = getMailSession();
		MimeMessage message = new MimeMessage( session );
		try {
			message.setFrom(new InternetAddress("abani@kbase.in"));
			message.addRecipient(Message.RecipientType.TO,
					new InternetAddress(recipientEmail));
			message.setSubject(subject);
			message.setContent(msg,
					"text/html" );
			System.out.println("Preparing to send mail");
			Transport.send(message);
			System.out.println("Sent message successfully....");
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}
